<template>
  <div class="coupon-page">
    <div class="coupon-tabs" style="display: none;">
      <div 
        class="tab-item" 
        :class="{ active: activeTab === 'valid' }"
        @click="changeTab('valid')"
      >
        有效优惠券
      </div>
      <div 
        class="tab-item" 
        :class="{ active: activeTab === 'used' }"
        @click="changeTab('used')"
      >
        已使用
      </div>
      <div 
        class="tab-item" 
        :class="{ active: activeTab === 'expired' }"
        @click="changeTab('expired')"
      >
        已过期
      </div>
    </div>
    
    <div class="coupon-list" v-if="!isLoading && coupons.length > 0">
      <div class="oneDiv" v-for="(coupon, index) in coupons" :key="index">
        <div class="iconDiv flexCS fw">
          <div class="text flexCS">
            <div class="img-wrapper">
              <div class="img-bg"></div>
              <img src="@/assets/images/user/couponicon.png" draggable="false" />
            </div>
            <span style="margin-left: 3px;">COP{{ formatNumber(coupon.amount) }}</span>
          </div>
          <div class="ellipsis2" >
            {{ coupon.code }}
          </div>
        </div>
        <div class="textDiv" >
          <div class="textdiv_left">
            <span class="infotxt">Periode validitas</span>
            <div class="text fw" style="margin-left: 3px; font-size: 13px; font-weight: normal;">
              {{ coupon.validityDate }}
            </div>
          </div>
          <div>
            <div class="usebtn" @click="useCoupon(coupon.id)">
              <div class="u-icon u-icon--right">
                <van-icon name="arrow" size="12" color="#eee"/>
              </div>
              <div style="font-size: 12px; color: rgb(246, 234, 222);">Menggunakan</div>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <div v-else-if="isLoading" class="loading-state">
      <div class="loading-spinner"></div>
      <div>Memuat...</div>
    </div>
    
    <NoData v-else text="No Data" />
    
    <div class="coupon-form" style="display: none;">
      <div class="form-title">优惠码兑换</div>
      <div class="form-content">
        <input
          type="text"
          v-model="couponCode"
          placeholder="请输入优惠码"
          class="coupon-input"
        />
        <button class="redeem-btn" @click="redeemCoupon">兑换</button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
// 导入所需的依赖
import { formatNumber } from '@/utils/public'
import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'

// 尝试导入vant，如果项目中没有安装，需要先安装
import NoData from '@/components/NoData.vue'
import { showToast } from 'vant'

// 定义优惠券接口
interface Coupon {
  id: string | number
  amount: string
  code: string
  validityDate: string
}

// 初始化数据
const router = useRouter()
const activeTab = ref('valid')
const coupons = ref<Coupon[]>([])
const coupons222 = ref<Coupon[]>([
  {
    id: 1,
    amount: '50,000',
    code: 'ADC AD92341 kupon',
    validityDate: '11:58 11/05/2025'
  },
  {
    id: 2,
    amount: '50,000',
    code: 'ADC AD92341 kupon',
    validityDate: '21:54 09/05/2025'
  }
])
const couponCode = ref('')
const isLoading = ref(false)

// 生命周期钩子
onMounted(() => {
  // 获取优惠券列表
  // getCoupons()
})

// 方法定义
function getCoupons() {
  isLoading.value = true
  
  // 模拟API请求 - 现在返回模拟数据
  setTimeout(() => {
    // 使用模拟数据
    coupons.value = [
      {
        id: 1,
        amount: '50,000',
        code: 'ADC AD92341 kupon',
        validityDate: '11:58 11/05/2025'
      },
      {
        id: 2,
        amount: '50,000',
        code: 'ADC AD92341 kupon',
        validityDate: '21:54 09/05/2025'
      }
    ]
    isLoading.value = false
  }, 500)
}

function changeTab(tab: string) {
  activeTab.value = tab
  getCoupons()
}

function redeemCoupon() {
  if (!couponCode.value) {
    // 提示用户输入优惠码
    return
  }
  
  // 兑换优惠券逻辑...
  couponCode.value = ''
}

function useCoupon(couponId: string | number) {
  // 使用优惠券
  // alert('Menggunakan kupon ID: ' + couponId)
  // 如果项目中有vant
  showToast('Menggunakan kupon ID: ' + couponId)
}
</script>

<style lang="less" scoped>
.coupon-page {
  min-height: 100vh;
  padding: 20px;
  background: url('@/assets/images/user/Topbg.png') no-repeat center center;
  background-size: cover;
}

.oneDiv {
  width: 100%;
  overflow: hidden;
  margin: 0 0 11px 0;
  background: url('@/assets/images/user/bgMes.png') no-repeat;
  background-size: 100% 100%;
  border-radius: 18px;
}

.iconDiv {
  overflow: hidden;
  padding: 17px 17px;
  background-color: rgba(254, 242, 230, .1);
  border-radius: 12px;
}

.text {
  display: flex;
  align-items: center;
  font-size: 18px;
  color: #f6e0ca;
}

.img-wrapper {
  width: 34px;
  height: 40px;
  position: relative;
  
  .img-bg {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-image: url('@/assets/images/user/couponicon.png');
    background-position: 0% 0%;
    background-size: 100% 100%;
    background-repeat: no-repeat;
  }
  
  img {
    width: 100%;
    height: 100%;
    object-fit: contain;
  }
}

.ellipsis2 {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  color: rgba(246, 224, 202, 0.7);
  margin-top: 3px;
  margin-left:30px;
  font-size: 16px; 
  font-weight: normal;
}

.flexCS {
  display: flex;
  align-items: center;
  justify-content: flex-start;
}

.fw {
  font-weight: bold;
}

.u-icon {
  &--right {
    margin-right: 2px;
  }
  
  &__icon {
    display: inline-flex;
    align-items: center;
  }
}

.textDiv {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 12px;
  .textdiv_left{
    display:flex;
  }
  .validity-info {
    display: flex;
    font-size: 13px;
    margin-top: 0;
    
    span {
      color: rgba(246, 224, 202, 0.6);
    }
    
    .text {
      margin-left: 3px;
      font-size: 13px;
      font-weight: normal;
      color: rgba(246, 224, 202, 0.6);
    }
  }
}

.btn-wrapper {
  .coupon-use-btn {
    display: flex;
    align-items: center;
    background: transparent;
    border: 0.5px solid rgba(153, 153, 153, 0.3);
    border-radius: 3px;
    padding: 3px 5px;
    cursor: pointer;
    
    .arrow-icon {
      display: inline-block;
      width: 13px;
      height: 13px;
      line-height: 13px;
      font-size: 13px;
      color: #f6eade;
      font-weight: normal;
      margin-right: 2px;
      
      &::before {
        content: '›';
        display: inline-block;
      }
    }
    
    span {
      font-size: 12px;
      color: #f6eade;
    }
    
    &:active {
      opacity: 0.8;
    }
  }
}

.loading-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 50px 0;
  color: rgba(246, 224, 202, 0.6);
  font-size: 14px;
}

.loading-spinner {
  width: 30px;
  height: 30px;
  border: 2px solid rgba(246, 224, 202, 0.2);
  border-top: 2px solid #f6e0ca;
  border-radius: 50%;
  margin-bottom: 10px;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.no-data-wrapper {
  margin: 50px 0;
}

.no-data {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 50px 0;
}

.no-data-text {
  color: rgba(246, 224, 202, 0.6);
  font-size: 14px;
}

.coupon-tabs {
  display: flex;
  margin-bottom: 20px;
  background-color: rgba(30, 27, 26, 0.5);
  border-radius: 8px;
  overflow: hidden;
  
  .tab-item {
    flex: 1;
    text-align: center;
    padding: 12px 0;
    color: #f6e0ca;
    font-size: 14px;
    transition: all 0.3s;
    cursor: pointer;
    
    &.active {
      background-color: rgba(246, 224, 202, 0.2);
      color: #fff;
    }
  }
}

.coupon-list {
  margin-bottom: 20px;
  padding-top: 20px;
}

.coupon-form {
  margin-bottom: 20px;
  padding: 15px;
  border-radius: 8px;
  background-color: rgba(30, 27, 26, 0.5);
  
  .form-title {
    color: #f6e0ca;
    font-size: 16px;
    margin-bottom: 15px;
  }
  
  .form-content {
    display: flex;
  }
  
  .coupon-input {
    flex: 1;
    background-color: rgba(255, 255, 255, 0.1);
    border: none;
    height: 40px;
    border-radius: 4px;
    padding: 0 15px;
    color: #fff;
    margin-right: 10px;
    
    &::placeholder {
      color: rgba(255, 255, 255, 0.5);
    }
  }
  
  .redeem-btn {
    width: 80px;
    height: 40px;
    background-color: #f6e0ca;
    border: none;
    border-radius: 4px;
    color: #333;
    font-weight: bold;
    cursor: pointer;
    
    &:active {
      opacity: 0.9;
    }
  }
}
.infotxt{
  color:#eee;
}
.usebtn{
  display:flex;
  align-items: center;
  justify-content: center;
  border-radius: 3px;
  border: 0.5px solid rgba(153, 153, 153, 0.3);
}
</style>
